// const loaderUtils = require('loader-utils');
// const fs = require('fs');

module.exports = function(content) {
  if (content && /\<template/.test(content)) {
    content = callMethods(content, [
      handlerClick,
      handlerCreated,
      handlerMounted,
      handlerMethods,
      handlerBeforeRouteLeave,
      handlerDestroyed,
      handlerDirective
    ]);
  }

  return content;
};

function callMethods(str, methodArr) {
  methodArr.forEach(item => {
    str = item(str);
  });
  return str;
}

function handlerClick(str) {
  let regex = /@click[^=]*\=\s*\"(\w+)[\"\(\s\']/g;
  let tempResult = null;
  let findStrs = [];
  while ((tempResult = regex.exec(str)) != null) {
    findStrs.push(tempResult[1]);
  }
  var code = 'this.$monitor.onClick(this);';
  findStrs.forEach(item => {
    let r = new RegExp('(' + item + '[^>{},;"\']+{)', 'g');
    str = str.replace(r, '$1' + code);
  });
  return str;
}

function handlerDirective(str) {
  const reg = /(<[^>\/\s]*input)/gi;
  // 过滤router-link
  const reglink = /(<[^>\/\s]*link)/gi;
  // 过滤<a href
  const regaTag = /(<a\b[^>]*\bhref="([^"]*)")/gi;
  str = str.replace(reg, '$1' + ' v-inputHandler');
  str = str.replace(reglink, '$1' + "  @click.native='_onHreflik()'");
  str = str.replace(regaTag, '$1' + "  @click='_onHreflik()'");
  return str;
}

function handlerCreated(str) {
  let regex = /(created.+{)/g;
  if (regex.test(str) && !/\/\/\s*created.+{/g.test(str)) {
    let code = '\n      this.$monitor.onCreate(this);';
    str = str.replace(regex, '$1' + code);
  } else {
    regex = /(export\s+default\s+{)/g;
    let code = '\n    created() {\n      this.$monitor.onCreate(this);\n    },';
    str = str.replace(regex, '$1' + code);
  }
  return str;
}

function handlerMounted(str) {
  let regex = /(mounted.+{)/g;
  if (regex.test(str) && !/\/\/\s*mounted.+{/g.test(str)) {
    let code = '\n      this.$monitor.onMounted(this);';
    str = str.replace(regex, '$1' + code);
  } else {
    regex = /(export\s+default\s+{)/g;
    let code =
      '\n    mounted() {\n      this.$monitor.onMounted(this);\n    },';
    str = str.replace(regex, '$1' + code);
  }
  return str;
}

function handlerMethods(str) {
  let regex = /(methods.+{)/g;
  let reglink = /(<[^>\/\s]*link)/gi;
  let regaTag = /(<a\b[^>]*\bhref="([^"]*)")/gi;
  if (regex.test(str) && !/\/\/\s*methods.+{/g.test(str)) {
    if (reglink.test(str) || regaTag.test(str)) {
      let code =
        '\n      _onHreflik() {      this.$monitor.onClick(this);\n      },\n      ';
      str = str.replace(regex, '$1' + code);
    }
  } else if (reglink.test(str) || regaTag.test(str)) {
    regex = /(export\s+default\s+{)/g;
    let code =
      '\n    methods: {\n      _onHreflik() {\n      this.$monitor.onClick(this);\n      },\n    },';
    str = str.replace(regex, '$1' + code);
  }
  return str;
}

function handlerBeforeRouteLeave(str) {
  let regex = /(beforeRouteLeave.+{)/g;
  if (regex.test(str) && !/\/\/\s*beforeRouteLeave.+{/g.test(str)) {
    let code = '\n      this.$monitor.onBeforeRouteLeave(this);';
    str = str.replace(regex, '$1' + code);
  } else {
    regex = /(export\s+default\s+{)/g;
    let code =
      '\n    beforeRouteLeave(to,from,next) {\n      this.$monitor.onBeforeRouteLeave(this); \n      next();\n    },';
    str = str.replace(regex, '$1' + code);
  }
  return str;
}

function handlerDestroyed(str) {
  let regex = /(destroyed.+{)/g;
  if (regex.test(str) && !/\/\/\s*destroyed.+{/g.test(str)) {
    let code = '\n      this.$monitor.onDestroy(this);';
    str = str.replace(regex, '$1' + code);
  } else {
    regex = /(export\s+default\s+{)/g;
    let code =
      '\n    destroyed() {\n      this.$monitor.onDestroy(this);\n    },';
    str = str.replace(regex, '$1' + code);
  }
  return str;
}
